<?php 

class Ext_Utility_File 
{
    public static function prepareDir($dir, $mode = 0777) 
    {
        if(!$dir)
        {
            return "";
        }
        
        if (substr(PHP_OS, 0, 3) == 'WIN')
        {
            $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir);
        }
        
        $stack = array(basename($dir));

        $path = null;
        while ( ($d = dirname($dir) ) ) {
            if ( !is_dir($d) ) {
                $stack[] = basename($d);
                $dir = $d;
            } else {
                $path = $d;
                break;
            }
        }
    
        if ( ( $path = realpath($path) ) === false ) 
            return false;
     
        $created = array();
        for ( $n = count($stack) - 1; $n >= 0; $n-- ) 
        {
            $s = $path . DIRECTORY_SEPARATOR. $stack[$n]; 
            if (is_dir($s))
            {
                continue;
            }
            
            //$oldumask=umask(0);
            if (!mkdir($s, $mode)) {
                for ( $m = count($created) - 1; $m >= 0; $m-- ) 
                    rmdir($created[$m]);
                return false;
            }
            //umask($oldumask);
            $created[] = $s;       
            $path = $s;
        }
        return true;
    }
    
    public static function preparePath($dir, & $path)
    {
        if(!$dir)
        {
            return "";
        }
        
        $dir = rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
        $path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
        self::prepareDir($dir.$path);
        
        $arr = scandir($dir.$path, 1);
        $maxid = (int)$arr['0'];
        if($maxid < 1) $maxid = 1;
        
        self::prepareDir($dir.$path.$maxid);
        $arr = scandir($dir.$path.$maxid);
        $thubmNUm = count($arr)-2;
        if($thubmNUm>800)
        {
            $maxid++;
        }
        
        $path .= $maxid;
        self::prepareDir($dir.$path);
        
        return $path;
    }
    
    public static function getImageType($nameWithDir)
    {
        if(!is_file($nameWithDir)) {
            return false;
        }
    
        $imagetypes = array('1'=>'gif','2'=>'jpg','3'=>'png','4'=>'swf','5'=>'psd','6'=>'bmp','7'=>'tiff','8'=>'tiff','9'=>'jpc','10'=>'jp2','11'=>'jpx','12'=>'jb2','13'=>'swc','14'=>'iff','15'=>'wbmp','16'=>'xbm',);
        
        $typeid = 0;
        $imagetype = '';
        if(function_exists('exif_imagetype')) {
            $typeid = exif_imagetype($nameWithDir);
            if($typeid > 0) {
                $imagetype = $imagetypes[$typeid];
            }
        } elseif (function_exists('getimagesize')) {
            $_tmps = getimagesize($nameWithDir);
            $typeid = (int) $_tmps[2];
            if($typeid > 0) {
                $imagetype = $imagetypes[$typeid];
            }
        } else {
            if(($fh = @fopen($nameWithDir, "rb"))) {
                $strInfo = unpack("C2chars", fread($fh,2));
                fclose($fh);
                $fileTypes = array(7790=>'exe',7784=>'midi',8297=>'rar',255216=>'jpg',7173=>'gif',6677=>'bmp',13780=>'png',);
                $imagetype = $fileTypes[intval($strInfo['chars1'] . $strInfo['chars2'])];
            }
        }
    
        return $imagetype;
    }
    
    public static function writeFile($file_name, $file_contents, $mode = 'wb')
    {
        self::prepareDir(dirname($file_name));
        
        if (($mode == 'w' || $mode == 'wb') && function_exists('file_put_contents'))
        {
            return file_put_contents($file_name, $file_contents);
        }
        else
        {
            $file_handler = @fopen($file_name, $mode);
            if ($file_handler)
            {
                $len=fwrite($file_handler, $file_contents);
                fclose($file_handler);
                return $len;
            }
            else
            {
                return false;
            }
        }
    }
    

}